package com.xigua.xcar.net.feedback;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.util.Log;

import com.bantouyan.json.Json;
import com.bantouyan.json.JsonObject;
import com.xigua.xcar.constant.ClientInfo;
import com.xigua.xcar.constant.CommonUtil;
import com.xigua.xcar.net.AbstractData;
import com.xigua.xcar.net.AbstractDataManager;

public class FeedbkDataManager extends AbstractDataManager {

	private static final String tag = "FeedbkDataManager";
	private int requestcode = 1105;
	private String content = null;
	private String contact = null;
	private String nickName = null;
	private FeedbackData mData = null;
	
	public FeedbkDataManager(Context context) {
		super(context);
	}
	
	public void setContent(String content){
		this.content = content;
	}
	
	public void setContact(String contact){
		this.contact = contact;
	}
	
	public void setNickName(String nickName){
		this.nickName = nickName;
	}
	
	@Override
	public List<NameValuePair> getRequestParam() {
		List<NameValuePair> postParamList = new ArrayList<NameValuePair>();
		BasicNameValuePair nameValuePair1  = new BasicNameValuePair("requestcode",""+requestcode);        
        postParamList.add(nameValuePair1);
        ClientInfo info = CommonUtil.getClientInfo(mContext);
        nameValuePair1 = new BasicNameValuePair("param1", nickName);
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param2", contact);
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param3", 
        									"" + info.getImei()+ ":" + 
        									info.getHsman() + ":" + 
        									info.getHstype() + ":" +
        									info.getRam() + ":" +
        									info.getWidth() + ":" +
        									info.getHeight() + ":" +
        									info.getDpi());
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param4", "");
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param5", content);
        postParamList.add(nameValuePair1);
		return postParamList;
	}

	@Override
	public AbstractData getData() {
		return mData;
	}

	@Override
	public AbstractData afterParseData(String ss) {
		Log.i(tag, "orign:ss:" + ss);
		String array[] = ss.split(";");
		if(null != array){
			if(null != array[0]){
				ss = array[0];
			}
		}
		ss = ss.replace("\n", "");
		if(ss.indexOf("{")!=-1){
			ss = ss.substring(ss.indexOf("{"));
		}
		if(ss.indexOf("}")!= -1){
			ss = ss.substring(0, ss.indexOf("}")+1);
		}
		Log.i(tag, "ss:" + ss);
		try{
			JsonObject jsonArray = (JsonObject)Json.parseJsonText(ss);
			mData = new FeedbackData();
			String content = jsonArray.getString("result");
			Log.i(tag, "content:" + content);
			if(null != content){
				mData.setContent(content);
			}else{
				mData.setContent("提交成功");
			}
		}catch(Exception e){
			mData = null;
		}
		return mData;
	}

}
